package com.hzf.material.admin.controller.manager;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.validator.LengthValidator;
import com.hzf.config.dao.model.ConfigRegion;
import com.hzf.config.dao.model.ConfigRegionExample;
import com.hzf.material.dao.model.MaterialArea;
import com.hzf.material.dao.model.MaterialAreaExample;
import com.hzf.material.rpc.api.MaterialAreaService;
import com.hzf.upms.common.constant.UpmsResult;
import com.hzf.upms.common.constant.UpmsResultConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe: 区域列表查询
 * creat_user: 白开水
 * creat_time: 2017/11/16   18:26
 **/
@Controller
@Api(value = "材料商区域区域管理", description = "材料商管理")
@RequestMapping("/manage/marketArea")
public class MaterialAreaController {
    private static Logger _log = LoggerFactory.getLogger(MaterialAreaController.class);

    @Autowired
    private MaterialAreaService materialAreaService;

    @Autowired
    private ApiService apiService;



    @ApiOperation(value = "区域列表首页")
    @RequiresPermissions("material:marketArea:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/materialArea/index.jsp";

    }

    @ApiOperation(value="基础区域列表")
    @RequiresPermissions("material:marketArea:read")
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        MaterialAreaExample materiaAreaExample = new MaterialAreaExample();
        if(!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)){
            materiaAreaExample.setOrderByClause(sort+" "+order);
        }
        List<MaterialArea> rows = materialAreaService.selectByExampleForOffsetPage(materiaAreaExample,offset,limit);
        for (int i = 0; i <rows.size() ; i++) {
            System.out.println(rows.get(i).getMarketListName()!=null);
        }
        long total = materialAreaService.countByExample(materiaAreaExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增区域")
    @RequiresPermissions("material:marketArea:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        return "/manage/materialArea/create.jsp";
    }

    @ApiOperation(value = "新增区域")
    @RequiresPermissions("material:marketArea:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(MaterialArea materiaArea){
        ComplexResult result = FluentValidator.checkAll()
                .on(materiaArea.getMarketAreaName(), new LengthValidator(1, 50, "区域名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        int count = materialAreaService.insertSelective(materiaArea);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除区域")
        @RequiresPermissions("material:marketArea:delete")
        @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
        @ResponseBody
        public Object delete(@PathVariable("ids") String ids) {
            int count = materialAreaService.deleteByPrimaryKeys(ids);
            return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }


    @ApiOperation(value = "修改区域")
    @RequiresPermissions("material:marketArea:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        MaterialArea materiaArea = materialAreaService.selectByPrimaryKey(id);
        modelMap.put("materiaArea", materiaArea);
        return "/manage/materialArea/update.jsp";
    }

    @ApiOperation(value = "修改区域")
    @RequiresPermissions("material:markettype:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, MaterialArea materiaArea) {
        ComplexResult result = FluentValidator.checkAll()
                .on(materiaArea.getMarketAreaName(), new LengthValidator(1, 50, "区域名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        materiaArea.setMarketAreaId(id);
        int count = materialAreaService.updateByPrimaryKeySelective(materiaArea);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }



}
